package com.common.base.utils

import android.content.pm.PackageManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.tbruyelle.rxpermissions3.Permission
import com.tbruyelle.rxpermissions3.RxPermissions
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer

/**
 * 权限申请工具类
 */
object RxPermissionHelper {

    fun request(activity: FragmentActivity, vararg permissions: String): Disposable {
        return RxPermissions(activity).request(*permissions).subscribe()
    }

    fun request(fragment: Fragment, vararg permissions: String): Disposable {
        return RxPermissions(fragment).request(*permissions).subscribe()
    }

    fun request(activity: FragmentActivity, vararg permissions: String, onNext: Consumer<Boolean>): Disposable {
        return RxPermissions(activity).request(*permissions).subscribe(onNext)
    }

    fun request(fragment: Fragment, vararg permissions: String, onNext: Consumer<Boolean>): Disposable {
        return RxPermissions(fragment).request(*permissions).subscribe(onNext)
    }

    fun requestEach(activity: FragmentActivity, vararg permissions: String): Disposable {
        return RxPermissions(activity).requestEach(*permissions).subscribe()
    }

    fun requestEach(fragment: Fragment, vararg permissions: String): Disposable {
        return RxPermissions(fragment).requestEach(*permissions).subscribe()
    }

    fun requestEach(activity: FragmentActivity, vararg permissions: String, onNext: Consumer<Permission>): Disposable {
        return RxPermissions(activity).requestEach(*permissions).subscribe(onNext)
    }

    fun requestEach(fragment: Fragment, vararg permissions: String, onNext: Consumer<Permission>): Disposable {
        return RxPermissions(fragment).requestEach(*permissions).subscribe(onNext)
    }

    fun checkPermission(permission: String): Boolean {
        val perm = AppUtils.getContext().checkCallingOrSelfPermission(permission)
        return perm == PackageManager.PERMISSION_GRANTED
    }

    fun requestEachList(activity: FragmentActivity, vararg permissions: String, onResult: (PermissionResult) -> Unit): Disposable {
        return RxPermissions(activity)
            .requestEach(*permissions)
            .subscribe { permission ->
                when {
                    permission.granted -> onResult(PermissionResult.Granted)
                    permission.shouldShowRequestPermissionRationale -> onResult(PermissionResult.Denied)
                    else -> onResult(PermissionResult.PermanentlyDenied)
                }
            }
    }

    fun requestAllOrNothing(activity: FragmentActivity, vararg permissions: String, onResult: (Boolean) -> Unit): Disposable {
        return RxPermissions(activity)
            .request(*permissions)
            .subscribe { allGranted ->
                onResult(allGranted)
            }
    }

}

enum class PermissionResult {
    Granted, Denied, PermanentlyDenied
}